import { CallFactory } from "../CallFactory"
import { Method, Request } from "../Reqest"
import { url } from "@kit.ArkTS"
import { BASE_URL, CALL_FACTORY, HANDLER_FUNCTION, HANDLER_HEADER, HEADER, MAP_INDEX, REST_SERVICE } from "../Constant"

export function GET(value: string) {
  return (target: any, name: string, propertyDescriptor: PropertyDescriptor) => {
    target[REST_SERVICE] = true
    propertyDescriptor.value = function (...args: any[]) {
      let callFactory = this[CALL_FACTORY] as CallFactory
      let build = Request.createBuild()
      build.url = url.URL.parseURL(value, this[BASE_URL])
      build.method = Method.GET
      build[HANDLER_FUNCTION](this[MAP_INDEX+name], args)
      let header = this[HEADER]?.get(name)
      if (header) {
        build[HANDLER_HEADER](header)
      }
      return callFactory.buildCall(build.build())
    }
  }
}